# encoding: utf-8
class ScoresController < ApplicationController
  # GET /scores
  # GET /scores.json
  def index
    @f_n=""
    @f_t = Exam.find(params[:eid])
    ##及格数
    @pp, @np, @pj =0, 0, 0
    ##不及格数
    #@np =0
    ##平均分
    #@pj=0

    if params[:type].nil?
      @scores = Score.find_all_by_exam_id(params[:eid], :include=>[:student])
      @f_n ="所有学生成绩"
      statistics @scores
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @scores }
      end
    else
      if !params[:u].nil?
        @us = Score.find_by_sql("select scores.* from scores inner join students on scores.student_id = students.id where scores.exam_id = " +@f_t.id.to_s+" and students.university in "+ params[:u].to_s.gsub("[", "(").gsub("]", ")").gsub("\"", "'"))
        @f_n << "  大学:"<< params[:u].to_s
      end
      if !params[:y].nil?
        @ys = Score.find_by_sql("select scores.* from scores inner join students on scores.student_id = students.id where scores.exam_id = " +@f_t.id.to_s+" and students.college in "+params[:y].to_s.gsub("[", "(").gsub("]", ")").gsub("\"", "'"))
        @f_n << "  学院:" << params[:y].to_s
      end
      if !params[:g].nil?
        @gs = Score.find_by_sql("select scores.* from scores inner join students on scores.student_id = students.id where scores.exam_id = " +@f_t.id.to_s+" and students.sgroup in "+ params[:g].to_s.gsub("[", "(").gsub("]", ")").gsub("\"", "'"))
        @f_n << "  分组:" << params[:g].to_s
      end

      if !params[:dy].nil? && !params[:dy].empty?
        @dy = Score.where("mark > ?", params[:dy].to_i).find_all_by_exam_id(@f_t.id)
        @f_n << "  分数大于:" << params[:dy].to_s
      end
      if !params[:xy].nil? && !params[:xy].empty?
        @xy = Score.where("mark < ?", params[:xy].to_i).find_all_by_exam_id(@f_t.id)
        @f_n << "  分数小于:" << params[:xy].to_s
      end

      ss =[]<< @us<< @ys<<@gs<<@dy<<@xy
      ss.delete(nil)
      if ss.size ==0
        @f_n ="所有学生成绩"
        @scores = Score.find_all_by_exam_id(params[:eid])
      else
        rs = ss[0]
        ss.each do |sss|
          rs = rs & sss
        end
        @scores = rs
        @f_n += " 的学生成绩"
      end
      statistics @scores
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @scores }
      end
    end
  end

  def statistics scores
    sum =0
    scores.each do |s|
      #if (!s.mark.nil?&&s.mark>=60)
      #  @pp+=1
      #else
      #  @np+=1
      #end
      !s.mark.nil?&&s.mark>=60 ? @pp+=1 : @np+=1
      sum += s.mark.nil? ? 0 : s.mark
    end
    @pj = '%.2f' % (sum.to_f/scores.size)
  end

  def submitmark
    begin
      Score.transaction do
        v = params[:marks]
        v.split("|").each do |mark|
          m = mark.split(":")
          if !m[1].nil?
            s=Score.find(m[0].to_i)
            s.mark=m[1].to_i
            s.save
          end
        end
      end
      render :text => "success"
    rescue => e
      logger.error e
      render :text => "failed"
    end
  end

  # GET /scores/1
  # GET /scores/1.json
  def show
    @score = Score.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @score }
    end
  end

  # GET /scores/new
  # GET /scores/new.json
  def new
    @score = Score.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @score }
    end
  end

  # GET /scores/1/edit
  def edit
    @score = Score.find(params[:id])
  end

  # POST /scores
  # POST /scores.json
  def create
    @score = Score.new(params[:score])

    respond_to do |format|
      if @score.save
        format.html { redirect_to @score, notice: 'Score was successfully created.' }
        format.json { render json: @score, status: :created, location: @score }
      else
        format.html { render action: "new" }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /scores/1
  # PUT /scores/1.json
  def update
    @score = Score.find(params[:id])

    respond_to do |format|
      if @score.update_attributes(params[:score])
        format.html { redirect_to @score, notice: 'Score was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /scores/1
  # DELETE /scores/1.json
  def destroy
    @score = Score.find(params[:id])
    @score.destroy

    respond_to do |format|
      format.html { redirect_to scores_url }
      format.json { head :ok }
    end
  end
end
